package com.example.controller;

import com.example.Result;
import com.example.annotation.AsyncMessageHandler;
import com.example.entity.StudentHealth;
import com.example.service.StudentHealthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "学生健康管理", description = "学生健康状态相关接口")
@RestController
@RequestMapping("/api/student/health")
public class StudentHealthController {

    @Autowired
    private StudentHealthService studentHealthService;

    @Operation(summary = "获取学生健康记录列表")
    @GetMapping("/records/{studentId}")
    public Result<List<StudentHealth>> getHealthRecords(
            @Parameter(description = "学生ID") @PathVariable Long studentId) {
        List<StudentHealth> records = studentHealthService.getHealthRecords(studentId);
        return Result.success(records);
    }

    @Operation(summary = "获取学生最新健康记录")
    @GetMapping("/latest/{studentId}")
    public Result<StudentHealth> getLatestHealthRecord(
            @Parameter(description = "学生ID") @PathVariable Long studentId) {
        StudentHealth record = studentHealthService.getLatestHealthRecord(studentId);
        return Result.success(record);
    }

    @Operation(summary = "添加健康记录")
    @PostMapping("/record")
    public Result<Boolean> addHealthRecord(
            @Parameter(description = "健康记录信息") @RequestBody StudentHealth health) {
        boolean success = studentHealthService.addHealthRecord(health);
        return success ? Result.success(true) : Result.error("添加健康记录失败");
    }

    @Operation(summary = "更新健康记录")
    @PutMapping("/record")
    @AsyncMessageHandler(queue = "health.update.queue")
    public Result<Boolean> updateHealthRecord(
            @Parameter(description = "健康记录信息") @RequestBody StudentHealth health) {
        boolean success = studentHealthService.updateHealthRecord(health);
        return success ? Result.success(true) : Result.error("更新健康记录失败");
    }

    @Operation(summary = "删除健康记录")
    @DeleteMapping("/record/{id}")
    public Result<Boolean> deleteHealthRecord(
            @Parameter(description = "记录ID") @PathVariable Long id) {
        boolean success = studentHealthService.deleteHealthRecord(id);
        return success ? Result.success(true) : Result.error("删除健康记录失败");
    }

    @Operation(summary = "计算BMI指数")
    @GetMapping("/bmi")
    public Result<Double> calculateBMI(
            @Parameter(description = "身高(cm)") @RequestParam double height,
            @Parameter(description = "体重(kg)") @RequestParam double weight) {
        double bmi = studentHealthService.calculateBMI(height, weight);
        return Result.success(bmi);
    }

    @Operation(summary = "获取健康建议")
    @GetMapping("/advice")
    public Result<String> getHealthAdvice(
            @Parameter(description = "BMI指数") @RequestParam double bmi) {
        String advice = studentHealthService.getHealthAdvice(bmi);
        return Result.success(advice);
    }

    @PutMapping
    @Operation(summary = "更新健康数据")
    @AsyncMessageHandler(queue = "health.update.queue")
    public boolean updateHealth(@RequestBody StudentHealth health) {
        return studentHealthService.updateHealth(health);
    }

    @PostMapping("/report")
    @Operation(summary = "生成健康报告")
    @PreAuthorize("hasRole('ADMIN') or hasRole('SUPER_ADMIN')")
    @AsyncMessageHandler(queue = "health.report.queue")
    public boolean generateReport(@RequestParam Long studentId) {
        return studentHealthService.generateReport(studentId);
    }
} 